package com.rede.didiok.user.modulelist.file.web;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rede.didiok.common.annotion.FeignSecurity.FeignSecurity;
import com.rede.didiok.common.dto.admin.SystemConfigDto;
import com.rede.didiok.common.dto.file.FileDto;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.utils.JsonUtils;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.file.FeignUtil;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.user.modulelist.file.dto.QiNiuCallback.QiNiuAuditCallback;
import com.rede.didiok.user.modulelist.file.entity.FileEntity;
import com.rede.didiok.user.modulelist.file.service.FileService;


/**
 * 文件上传接口 【总的文件接口，需要调用本地文件、七牛云、Minio上传服务】
 * @author Rede
 * @date 2023-08-29
 */
@RestController
@RequestMapping("/file/feign/file")
@Slf4j
public class FileController {

    @Autowired
    private FeignUtil feignUtil;
    @Resource
    private FileService fileService;

    /**
     * 截图上传
     * @param file
     * @return
     */
    @RequestMapping(value = "/cropperPicture", method = RequestMethod.POST)
    public String cropperPicture(@RequestParam("file") MultipartFile file) {
        return fileService.cropperPicture(file);
    }

    /**
     * 获取用户最近上传的文件
     * @param fileVO
     * @return
     */
    @RequestMapping(value = "/getUserRecentlyUploadFile", method = RequestMethod.POST)
    public String getUserRecentlyUploadFile(@RequestBody FileDto fileVO) {
        return ResultUtil.successWithData(fileService.getUserRecentlyUploadFile(fileVO));
    }

    /**
     * 通过fileIds获取图片信息接口
     * fileIds 获取文件信息的ids
     * code ids用什么分割的，默认“,”
     *
     * @return
     */
    @GetMapping("/getPicture")
    public String getPicture(
             @RequestParam(name = "fileIds", required = false) String fileIds,
             @RequestParam(name = "code", required = false) String code) {
        return fileService.getPicture(fileIds, code);
    }

    /**
     * 多文件上传
     * 上传图片接口   传入 userId sysUserId ,有那个传哪个，记录是谁传的,
     * projectName 传入的项目名称如 base 默认是base
     * sortName 传入的模块名， 如 admin，user ,等，不在数据库中记录的是不会上传的
     *
     * @return
     */
    @PostMapping("/pictures")
    public synchronized Object uploadPics(HttpServletRequest request, List<MultipartFile> filedatas) {
        // 获取系统配置文件
        SystemConfigDto systemConfig = feignUtil.getSystemConfig();
        return fileService.batchUploadFile(request, filedatas, systemConfig);
    }

    /**
     * 视频文件上传
     * 上传视频接口   传入 userId sysUserId ,有那个传哪个，记录是谁传的,
     * projectName 传入的项目名称如 base 默认是base
     * sortName 传入的模块名， 如 admin，user ,等，不在数据库中记录的是不会上传的
     *
     * @return
     */
    @PostMapping("/uploadVideo")
    public synchronized Object uploadVideo(HttpServletRequest request, MultipartFile file) {
        // 获取系统配置文件
        SystemConfigDto systemConfig = feignUtil.getSystemConfig();
        return fileService.uploadVideoFile(request, file, systemConfig);
    }

    /**
     * 通过URL将图片上传到自己服务器中【主要用于Github和Gitee的头像上传】
     *
     * @param fileDto
     * @param result
     * @return
     */
    @PostMapping("/uploadPicsByUrl")
    public Object uploadPicsByUrl(@Validated({GetList.class}) @RequestBody FileDto fileDto, BindingResult result) {
        return fileService.uploadPictureByUrl(fileDto);
    }


    /**
     * Ckeditor图像中的图片上传
     *
     * @return
     */
    @RequestMapping(value = "/ckeditorUploadFile", method = RequestMethod.POST)
    public Object ckeditorUploadFile(HttpServletRequest request) {
        return fileService.ckeditorUploadFile(request);
    }

    /**
     * Ckeditor复制的图片上传
     *
     * @return
     */
    @RequestMapping(value = "/ckeditorUploadCopyFile", method = RequestMethod.POST)
    public synchronized Object ckeditorUploadCopyFile() {
        return fileService.ckeditorUploadCopyFile();
    }

    /**
     * Ckeditor工具栏 “插入\编辑超链接”的文件上传
     *
     * @return
     */
    @RequestMapping(value = "/ckeditorUploadToolFile", method = RequestMethod.POST)
    public Object ckeditorUploadToolFile(HttpServletRequest request) {
        return fileService.ckeditorUploadToolFile(request);
    }

    /**
     * 获取文件列表【用于素材管理】
     * @param fileVO
     * @return
     */
    @FeignSecurity
    @RequestMapping(value = "/getFileList", method = RequestMethod.POST)
    public String getFileList(@Validated({GetList.class}) @RequestBody FileDto fileDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return ResultUtil.successWithData(fileService.getFileList(fileDto));
    }

    @FeignSecurity
    @RequestMapping(value = "/batchDeleteFile", method = RequestMethod.POST)
    public String batchDeleteFile(@RequestBody List<String> fileUidList) {
        log.info("批量删除源文件, fileUidList: {}", JsonUtils.objectToJson(fileUidList));
        return ResultUtil.successWithData(fileService.batchDeleteFile(fileUidList));
    }

    @FeignSecurity
    @RequestMapping(value = "/batchDeleteBySystemConfig", method = RequestMethod.POST)
    public String batchDeleteBySystemConfig(@RequestBody SystemConfigDto systemConfigDto) {
        log.info("批量删除源文件, systemConfigDto: {}", JsonUtils.objectToJson(systemConfigDto));
        return ResultUtil.successWithData(fileService.batchDeleteBySystemConfig(systemConfigDto));
    }


    @RequestMapping(value = "/qiNiuAuditCallback", method = RequestMethod.POST)
    public String qiNiuAuditCallback(@RequestBody QiNiuAuditCallback qiNiuAuditCallback) {
        log.info("[qiNiuAuditCallback] 七牛云审核回调， request: {}", JsonUtils.objectToJson(qiNiuAuditCallback));
        return fileService.qiNiuAuditCallback(qiNiuAuditCallback);
    }

    /**
     * 获取文件
     * @param fileVO
     * @return
     */
    @FeignSecurity
    @ApiOperation(value = "根据条件获取文件", notes = "根据条件获取文件")
    @RequestMapping(value = "/getFileListMap", method = RequestMethod.POST)
    public Map<String, FileEntity> getFileListMap(@RequestBody FileDto fileVO) {
        IPage<FileEntity> fileIPage = fileService.getFileList(fileVO);
        List<FileEntity> fileList = fileIPage.getRecords();
        Map<String, FileEntity> fileMap = new HashMap<>();
        fileList.forEach(item -> {
            fileMap.put(item.getUid(), item);
        });
        return fileMap;
    }

    /**
     * 查询指定条件下的文件列表
     * @param fileDto
     * @return
     */
    @PostMapping("/listFileByPage")
    R getFilePage(@RequestBody FileDto fileDto){
        return R.ok().setData(fileService.getFilePage(fileDto));
    }

    /**
     * 根据 fileUidList 查询文件
     * @param fileUidList
     * @return
     */
    @PostMapping("/listFileByUids")
    R listFileByUids(@RequestBody List<String> fileUidList){
        return R.ok().setData(fileService.listFileByUids(fileUidList));
    }
}

